from imgui_bundle import imgui, immapp,ImVec4,ImVec2
from dataclasses import dataclass,field
from typing import List
from uuid import uuid4
import ctypes
# set dpi
ctypes.windll.user32.SetProcessDPIAware(2)

@dataclass
class Task:
    id: str
    text: str
    done: bool = False
    @staticmethod
    def new(text:str):
        return Task(uuid4().hex,text)
@dataclass
class State:
    userinput: str = ""
    tasks: List[Task] = field(default_factory=list)
    search: str = ""

state = State()
run = False
def run_once():
    io = imgui.get_io()
    msyh = io.fonts.add_font_from_file_ttf(r"C:\Windows\Fonts\msyh.ttc", 16)
    io.font_default = msyh


def gui():
    global run
    if not run:
        run_once()
        run = True
    # imgui.begin("Todo")
    avail = imgui.get_content_region_avail().x
    imgui.set_next_item_width(avail-60)
    _, state.userinput = imgui.input_text(
        "##New Task", state.userinput)
    imgui.same_line()
    if imgui.button("Add") and state.userinput:
        state.tasks.append(Task.new(state.userinput))
        state.userinput = ""
    imgui.spacing()
    imgui.set_next_item_width(avail-60)

    changed,state.search = imgui.input_text("Search",state.search)
    imgui.separator()
    for t in state.tasks:
        if not state.search in t.text:
            continue
        imgui.push_id(t.id)
        click,t.done =  imgui.checkbox(f"##done_{t.id}",t.done)
        imgui.same_line()
        if t.done:
            imgui.push_style_color(0,ImVec4(0.5,0.5,0.5,1))
        imgui.text_unformatted(t.text)
        if t.done:
            imgui.pop_style_color()

        imgui.same_line(avail)
        if imgui.button("x"):
            state.tasks.remove(t)
        imgui.pop_id()
    # imgui.end()

immapp.run(gui)
